package com.ouyang.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.ouyang.dto.HcEventDTO;
import com.ouyang.mapper.MonitorPoolMapper;
import com.ouyang.mapper.po.MonitorPoolPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class HkService {

    public static final String ARTEMIS_PATH = "/artemis";

    public static final String QUERY_PATH = "/api/eventService/v1/eventSubscriptionView";

    public static final String ADD_PATH = "/api/eventService/v1/eventSubscriptionByEventTypes";

    public static final String DELETE_PATH = "/api/eventService/v1/eventUnSubscriptionByEventTypes";

    @Autowired
    private MonitorPoolMapper monitorPoolMapper;

    //131330,131331,131612,131613,589825,196355,851969,131588,
    // 131585,131586,131587,131590,131593,131592,131591,131594,
    // 131595,131664,131665,131596,131603,131605,131597,131666,
    // 131609,131598,131599,131600,131601,131602,131604,131606,
    // 131607,131611,192518,192517,192516,192515,192514,192513,930335
    public String addEventType(HcEventDTO hcEventDTO) {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("eventTypes", hcEventDTO.eventType);
        jsonBody.put("eventDest", hcEventDTO.getCallBackUrl());//"http://111.231.25.58:8080/eventRcv"
        jsonBody.put("subType", 0);
        jsonBody.put("eventLvl", new int[]{0});
        String body = jsonBody.toJSONString();
        return sendHk(ADD_PATH, body);
    }

    public String deleteEventType(HcEventDTO hcEventDTO) {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("eventTypes", hcEventDTO.eventType);
        String body = jsonBody.toJSONString();
        return sendHk(DELETE_PATH, body);
    }


    public String sendHk(String url, String body) {
        ArtemisConfig.host = "222.180.210.210:1443"; // 平台的ip端口
        ArtemisConfig.appKey = "24572153";  // 密钥appkey
        ArtemisConfig.appSecret = "kLg35g6kp2VAQ1c3vM62";// 密钥appSecret
        final String ARTEMIS_PATH = "/artemis";
        final String urlApi = ARTEMIS_PATH + url;
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", urlApi);//根据现场环境部署确认是http还是https
            }
        };
        String contentType = "application/json";
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);// post请求application/json类型参数
        return result;
    }

    public String queryEventType() {
        return sendHk(QUERY_PATH, null);
    }


    //hc告警回调事件
    /**
     * 区域入侵 131588
     人员聚集 131593
     进入区域 131586
     火点检测 192515
     烟火检测 192514
     烟雾检测 192513
     */
    /**
     * {
     * "method": "OnEventNotify",
     * "params": {
     * "ability": "event_rule",
     * "events": [{
     * "data": {
     * "dataType": "behavioralAnalysis",
     * "recvTime": "2017-04-22T15:39:01+08:00",
     * "sendTime": "2017-04-22T15:39:01+08:00",
     * "dateTime": "2017-04-22T15:39:01+08:00",
     * "ipAddress": "10.19.134.11",
     * "portNo": 80,
     * "channelID": 1,
     * "eventType": "fielddetection",
     * "eventDescription": "fielddetection",
     * "fielddetection": [{
     * "targetAttrs": {
     * "imageServerCode": "3212234",
     * "deviceIndexCode": "1568556",
     * "cameraIndexCode": "1235415",
     * "channelName": "tongdao1",
     * "cameraAddress": "杭州市西兴",
     * "longitude": 116.39737,
     * "latitude": 116.39737
     * },
     * "imageUrl": "http://10.3.1.12:8080/xxx",
     * "duration": 120,
     * "sensitivityLevel": 20,
     * "rate": 30,
     * "detectionTarget": 1,
     * "regionCoordinatesList": [{
     * "positionX": 0.901,
     * "positionY": 0.536
     * },
     * {
     * "positionX": 0.901,
     * "positionY": 0.536
     * }
     * ]* 				}]* 			},
     * "eventId": "BE26E09F-0C6C-4EF9-BE2B-27007B261731",
     * "eventType": 131588,
     * "happenTime": "2019-01-02T15:17:24.000+08:00",
     * "srcIndex": "da107dd1989e44978a5efebe73d6e979",
     * "srcName": "浙江杭州",
     * "srcType": "camera",
     * "status": 0,
     * "timeout": 0
     * }],
     * "sendTime": "2019-01-02T15:19:59.857+08:00"* 	}
     * }
     *
     * @param json
     */
    public void eventRcv(String json) {
        JSONObject jsonObject = JSONObject.parseObject(json);
        JSONObject paramsObject = jsonObject.getJSONObject("params");
        JSONArray eventsArray = paramsObject.getJSONArray("events");
        for(int i=0;i<eventsArray.size();i++){
            JSONObject event=eventsArray.getJSONObject(i);
            String eventType=event.getString("eventType");
            switch (eventType){
                case "131588":
                    event131588(event);
                    break;
                case "131593":
                    event131593(event);
                    break;
                case "131586":
                    event131586(event);
                    break;
                case "192515":
                    event192515(event);
                    break;
                case "192514":
                    event192514(event);
                    break;
                case "192513":
                    event192513(event);
                    break;
                default:
                    break;
            }

        }
    }


    //区域检测
    public void event131588(JSONObject jsonObject){
        MonitorPoolPO monitorPoolPO=new MonitorPoolPO();
        String monitorCode=jsonObject.getString("srcIndex");
        String machineId= monitorPoolMapper.queryMachineIdByMachineCode(monitorCode);
        JSONArray jsonArray=jsonObject.getJSONObject("data").getJSONArray("fielddetection");
        String monitorPhoto=jsonArray.getJSONObject(0).getString("imageUrl").replace("172.16.0.107:6120","222.180.210.210:6040");

        monitorPoolPO.setMonitorType("131588");
        monitorPoolPO.setMachineCode(monitorCode);
        monitorPoolPO.setModelId("131588");
        monitorPoolPO.setMachineId(machineId);
        monitorPoolPO.setMonitorPhoto(monitorPhoto);
        monitorPoolPO.setMpId(String.valueOf(System.currentTimeMillis()));
        monitorPoolMapper.addMonitorPool(monitorPoolPO);

    }
    //人员聚集
    public void event131593(JSONObject jsonObject){
        MonitorPoolPO monitorPoolPO=new MonitorPoolPO();
        String monitorCode=jsonObject.getString("srcIndex");
        String machineId= monitorPoolMapper.queryMachineIdByMachineCode(monitorCode);
        JSONArray jsonArray=jsonObject.getJSONObject("data").getJSONArray("group");
        String monitorPhoto=jsonArray.getJSONObject(0).getString("imageUrl").replace("172.16.0.107:6120","222.180.210.210:6040");

        monitorPoolPO.setMonitorType("131593");
        monitorPoolPO.setMachineCode(monitorCode);
        monitorPoolPO.setModelId("131593");
        monitorPoolPO.setMachineId(machineId);
        monitorPoolPO.setMonitorPhoto(monitorPhoto);
        monitorPoolPO.setMpId(String.valueOf(System.currentTimeMillis()));
        monitorPoolMapper.addMonitorPool(monitorPoolPO);

    }
    //进入区域
    public void event131586(JSONObject jsonObject){
        MonitorPoolPO monitorPoolPO=new MonitorPoolPO();
        String monitorCode=jsonObject.getString("srcIndex");
        String machineId= monitorPoolMapper.queryMachineIdByMachineCode(monitorCode);
        JSONArray jsonArray=jsonObject.getJSONObject("data").getJSONArray("regionEntrance");
        String monitorPhoto=jsonArray.getJSONObject(0).getString("imageUrl").replace("172.16.0.107:6120","222.180.210.210:6040");

        monitorPoolPO.setMonitorType("131586");
        monitorPoolPO.setMachineCode(monitorCode);
        monitorPoolPO.setModelId("131586");
        monitorPoolPO.setMachineId(machineId);
        monitorPoolPO.setMonitorPhoto(monitorPhoto);
        monitorPoolPO.setMpId(String.valueOf(System.currentTimeMillis()));
        monitorPoolMapper.addMonitorPool(monitorPoolPO);
    }
    //火点检测
    public void event192515(JSONObject jsonObject){
        MonitorPoolPO monitorPoolPO=new MonitorPoolPO();
        String monitorCode=jsonObject.getString("srcIndex");
        String machineId= monitorPoolMapper.queryMachineIdByMachineCode(monitorCode);
        JSONArray jsonArray=jsonObject.getJSONObject("data").getJSONArray("fireDetection");
        String monitorPhoto=jsonArray.getJSONObject(0).getString("imageUrl").replace("172.16.0.107:6120","222.180.210.210:6040");

        monitorPoolPO.setMonitorType("192515");
        monitorPoolPO.setMachineCode(monitorCode);
        monitorPoolPO.setModelId("192515");
        monitorPoolPO.setMachineId(machineId);
        monitorPoolPO.setMonitorPhoto(monitorPhoto);
        monitorPoolPO.setMpId(String.valueOf(System.currentTimeMillis()));
        monitorPoolMapper.addMonitorPool(monitorPoolPO);
    }
    //烟火检测
    public void event192514(JSONObject jsonObject){
        MonitorPoolPO monitorPoolPO=new MonitorPoolPO();
        String monitorCode=jsonObject.getString("srcIndex");
        String machineId= monitorPoolMapper.queryMachineIdByMachineCode(monitorCode);
        JSONArray jsonArray=jsonObject.getJSONObject("data").getJSONArray("smokeAndFireDetection");
        String monitorPhoto=jsonArray.getJSONObject(0).getString("imageUrl").replace("172.16.0.107:6120","222.180.210.210:6040");

        monitorPoolPO.setMonitorType("192515");
        monitorPoolPO.setMachineCode(monitorCode);
        monitorPoolPO.setModelId("192515");
        monitorPoolPO.setMachineId(machineId);
        monitorPoolPO.setMonitorPhoto(monitorPhoto);
        monitorPoolPO.setMpId(String.valueOf(System.currentTimeMillis()));
        monitorPoolMapper.addMonitorPool(monitorPoolPO);
    }
    //烟雾检测
    public void event192513(JSONObject jsonObject){
        MonitorPoolPO monitorPoolPO=new MonitorPoolPO();
        String monitorCode=jsonObject.getString("srcIndex");
        String machineId= monitorPoolMapper.queryMachineIdByMachineCode(monitorCode);
        JSONArray jsonArray=jsonObject.getJSONObject("data").getJSONArray("smokeDetection");
        String monitorPhoto=jsonArray.getJSONObject(0).getString("imageUrl").replace("172.16.0.107:6120","222.180.210.210:6040");

        monitorPoolPO.setMonitorType("192513");
        monitorPoolPO.setMachineCode(monitorCode);
        monitorPoolPO.setModelId("192513");
        monitorPoolPO.setMachineId(machineId);
        monitorPoolPO.setMonitorPhoto(monitorPhoto);
        monitorPoolPO.setMpId(String.valueOf(System.currentTimeMillis()));
        monitorPoolMapper.addMonitorPool(monitorPoolPO);
    }

}
